import { wxRequest} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    devId: '',
    imgUrl: app.globalData.imgUrl,
    userInfo: wx.getStorageSync('userInfo') || {},
    uploadUrl: wx.getStorageSync('uploadUrl'),
    toplist:[],
    list: [],
      typeId: 0,
      correct: 0
  },

  onLoad: function(options) {
    let devId = options.devId;
    if(devId) {
      this.setData({ devId });
    }else{
      devId = "";
    }
    if (options.note) {
      let isMini = options.note == '31_ky_rgj_2' || options.note == '31_ky_rgj_4'
      wx.setStorageSync('isMini', isMini)
    }
    if(options.typeId) {
      this.setData({
        typeId: options.typeId,
        list: [
          {
            name: '调色记录',
            url: '/pages/color_recode/index?devId='+devId,
            auth: 'dyeRecord:view'
          },
          {
            name: '换料',
            url: '/pages/color_replace_do/index?devId='+devId,
            auth: 'dyeReplace:view'
          },
          {
            name: '换料记录',
            url: '/pages/color_replace/index?devId='+devId,
            auth: 'dyeReplaceRecord:view'
          },
          {
            name: '排队状态',
            url: '/pages/queue_state/index?devId='+devId,
            auth: 'dyeQueue:view'
          },
          {
            name: '染膏余量',
            url: '/pages/color_residue/index?devId='+devId,
            auth: 'dyeVolume:view'
          },{
            name: '设备检测',
            url: '/pages/colors_check/index?devId='+devId,
            auth: 'device:check'
          },{
            name: '设备说明',
            url: '/pages/about_device/list',
            auth: 'device:about:mgt'
          },{
            name: '自动校准',
            url: '/pages/dye_automatic/correct',
            auth: 'device:automatic:correct'
          },{
            name: '设备校准',
            url: '/pages/dye_correc/correct?devId='+devId,
            auth: 'correct:view'
          },
          {
            name: '校准记录',
            url: '/pages/dye_correc/index?devId='+devId,
            auth: 'dyeCorrect:view'
          }
        ]
      })
      wx.setStorageSync('currentTypeId', options.typeId)
    }
    if(options.correct && options.correct == 1) {
      this.setData({
        correct: options.correct,
        list: [
        {
          name: '调色记录',
          url: '/pages/color_recode/index?devId='+devId,
          auth: 'dyeRecord:view'
        },
        {
          name: '换料',
          url: '/pages/color_replace_do/index?devId='+devId,
          auth: 'dyeRecord:view'
        },
        {
          name: '换料记录',
          url: '/pages/color_replace/index?devId='+devId,
          auth: 'dyeRecord:view'
        }, 
        {
          name: '排队状态',
          url: '/pages/queue_state/index?devId='+devId,
          auth: 'dyeQueue:view'
        },
        {
          name: '设备校准',
          url: '/pages/dye_correc/correct?devId='+devId,
          auth: 'correct:view'
        },
        {
          name: '校准记录',
          url: '/pages/dye_correc/index?devId='+devId,
          auth: 'dyeCorrect:view'
        },{
          name: '染膏余量',
          url: '/pages/color_residue/index?devId='+devId,
          auth: 'dyeVolume:view'
        },{
          name: '设备检测',
          url: '/pages/colors_check/index?devId='+devId,
          auth: 'device:check'
        },{
          name: '设备说明',
          url: '/pages/about_device/list',
          auth: 'device:about:mgt'
        }]
      })
    }
    
  },

  onShow:function(){
    this.getUserInfo(); 
  },

//页面跳转
toPage:function(e){
  var url = e.currentTarget.dataset.url;
  wx.navigateTo({
    url,
  })
},


  //跳转到订单详情
  toOrder: function(e) {
    var status = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/order/index?status=' + status,
    })
  },

  //去设置
  toSetting:function(){
      wx.navigateTo({
        url: '/pages/menber/setting/index',
      })
  },

  //获取用户信息
  getUserInfo: function () {
    wx.removeStorageSync('userInfo');
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/user/getLoginUser',
      method: 'get',
    }, (res) => {
      wx.hideLoading() 
      this.setData({
        userInfo: res.vo
      })
      wx.setStorageSync('userInfo', res.vo);
      this.getAuthorizedItem();
      console.log(res, '用户信息')
    })
  },

  //获取当前登录用户的所有权限
  getAuthorizedItem: function () {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/structure/getAuthorizedItem',
      method: 'get',
    }, (res) => {
      wx.hideLoading()
      console.log(res)
      var list = res.list;
      var authorized = {}
      list.map(item => {
        var obj = {
          [item.authCode]: item.name
        }
        authorized = Object.assign(authorized, obj)
      })
      that.$globalSetData({
        permissions: authorized
      })
      wx.setStorageSync('authorized', authorized);

    })
  },




  //商品关注
  arrGds: function () {
    wx.navigateTo({
      url: '/pages/attention/goods',
    })
  },

  arrStore: function () {
    wx.navigateTo({
      url: '/pages/attention/store',
    })
  },
})